<template>
	<div class="game-container report-container">
		<div class="params">
			<el-form ref="form" :inline="true" :model="queryData">
				<el-form-item label="变更时间:">
					<el-date-picker
						v-model="formTime.time"
						size="medium"
						:picker-options="pickerOptions"
						format="yyyy-MM-dd HH:mm:ss"
						type="datetimerange"
						range-separator="-"
						start-placeholder="开始日期"
						end-placeholder="结束日期"
						align="right"
						:clearable="false"
						:default-time="defaultTime"
					></el-date-picker>
				</el-form-item>
				<el-form-item label="变更类型:">
					<el-select
						v-model="queryData.changeType"
						style="width: 270px"
						clearable
						placeholder="默认选择全部"
						:popper-append-to-body="false"
					>
						<el-option
							v-for="item in domainRecordType"
							:key="item.codes"
							:label="item.description"
							:value="item.code"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="操作人:">
					<el-input
						v-model="queryData.operatorBy"
						clearable
						size="medium"
						style="width: 197px"
						placeholder="请输入"
						@keyup.enter.native="enterSearch"
					></el-input>
				</el-form-item>
				<el-form-item style="margin-left: 30px">
					<el-button
						type="primary"
						icon="el-icon-search"
						:disabled="loading"
						size="medium"
						@click="search"
					>
						查询
					</el-button>
					<el-button
						icon="el-icon-refresh-left"
						:disabled="loading"
						size="medium"
						@click="reset"
					>
						重置
					</el-button>
				</el-form-item>
			</el-form>
		</div>
		<div class="view-container dealer-container">
			<div class="content">
				<el-table
					v-loading="loading"
					element-loading-spinner="el-icon-loading"
					border
					size="mini"
					class="small-size-table"
					:data="dataList"
					style="width: 100%"
					:header-cell-style="getRowClass"
					@sort-change="changeTableSort"
				>
					<el-table-column
						prop="operatorAt"
						width="340"
						align="center"
						label="操作时间"
					>
						<template slot-scope="scope">
							<span>{{ scope.row.operatorAt || '-' }}</span>
						</template>
					</el-table-column>
					<el-table-column
						prop="changeObj"
						align="center"
						label="变更对象"
						width="280"
					>
						<template
							v-if="
								scope.row.changeObj || scope.row.changeObj === 0
							"
							slot-scope="scope"
						>
							<span>{{ scope.row.changeObj }}</span>
						</template>
						<span v-else>-</span>
					</el-table-column>
					<el-table-column
						prop="changeType"
						align="center"
						label="变更类型"
						width="380"
					>
						<template
							v-if="
								scope.row.changeType ||
									scope.row.changeType === 0
							"
							slot-scope="scope"
						>
							<span>
								{{
									typeFilter(
										scope.row.changeType,
										'domainRecordType'
									)
								}}
							</span>
						</template>
						<span v-else>-</span>
					</el-table-column>
					<el-table-column
						align="center"
						prop="changeFront"
						label="变更前"
						width="180"
					>
						<template slot-scope="scope">
							<span>{{ scope.row.changeFront || '-' }}</span>
						</template>
					</el-table-column>
					<el-table-column
						align="center"
						prop="changeAfter"
						label="变更后"
						width="190"
					>
						<template slot-scope="scope">
							<span>{{ scope.row.changeAfter || '-' }}</span>
						</template>
					</el-table-column>
					<el-table-column
						prop="operatorBy"
						align="center"
						width="280"
						label="操作人"
					>
						<template slot-scope="scope">
							<span>{{ scope.row.operatorBy || '-' }}</span>
						</template>
					</el-table-column>
				</el-table>
				<!-- 分页 -->
				<el-pagination
					:current-page.sync="pageNum"
					class="pageValue"
					background
					layout="total, sizes,prev, pager, next, jumper"
					:page-size="pageSize"
					:page-sizes="pageSizes"
					:total="total"
					@current-change="handleCurrentChange"
					@size-change="handleSizeChange"
				></el-pagination>
			</div>
		</div>
	</div>
</template>

<script>
import list from '@/mixins/list'
import { routerNames } from '@/utils/consts'
import dayjs from 'dayjs'
const end = dayjs()
	.endOf('day')
	.valueOf()
const start = dayjs()
	.startOf('day')
	.valueOf()
export default {
	name: routerNames.clientLog,
	components: {},
	mixins: [list],
	data() {
		this.loadData = this.throttle(this.loadData, 1000)
		return {
			queryData: {},
			formTime: {
				time: [start, end]
			},
			dataList: [],
			changeType: [],
			QueryGameList: [],
			client: [],
			operatePage: [],
			title: ''
		}
	},
	computed: {
		domainRecordType() {
			return this.globalDics.domainRecordType || []
		}
	},
	created() {
		this.getEnums()
	},
	mounted() {},
	methods: {
		loadData() {
			const [startTime, endTime] = this.formTime.time || []
			let params = {
				...this.queryData,
				updatedStartTime: startTime
					? dayjs(startTime).format('YYYY-MM-DD HH:mm:ss')
					: undefined,
				updatedEndTime: endTime
					? dayjs(endTime).format('YYYY-MM-DD HH:mm:ss')
					: undefined
			}
			if (!params.updatedStartTime || !params.updatedEndTime) {
				this.$message({
					message: '操作时间参数必传',
					type: 'info'
				})
				return
			}
			params = {
				...this.getParams(params)
			}
			this.loading = true

			this.$api
				.getmemberDomaindomainRecordList(params)
				.then((res) => {
					if (res?.code === 200) {
						const { record, totalRecord } = res.data || {}
						this.dataList = Array.isArray(record)
							? Object.freeze(record)
							: []
						this.total = totalRecord || 0
					} else {
						this.$message({
							message: res?.msg || '接口异常',
							type: 'error'
						})
					}
					this.loading = false
				})
				.catch(() => {
					this.loading = false
				})
		},
		dataFormatbefore(val) {
			if (val > 1) {
				for (let i = 0; i < this.QueryGameList.length; i++) {
					const element = this.QueryGameList[i]
					if (element.gameId === val) {
						return element.gameName
					} else {
						return val
					}
				}
			} else {
				return val
			}
		},
		dataFormat(val) {
			if (val > 1) {
				for (let i = 0; i < this.QueryGameList.length; i++) {
					const element = this.QueryGameList[i]
					if (element.gameId === val) {
						return element.gameName
					}
				}
			}
			return val
		},

		getEnums() {
			this.$api.operateConfigClientRecordQueryEnumsAPI().then((res) => {
				if (res?.code === 200) {
					this.changeType = res.data?.changeType || []
					this.client = res.data?.client || []
					this.operatePage = res.data?.operatePage || []
				}
			})
		},
		changeTableSort({ column, prop, order }) {
			if (prop === 'createdAt') {
				prop = 0
			}
			this.pageNum = 1

			this.queryData.orderKey = prop
			if (order === 'ascending') {
				// 升序
				this.queryData.orderType = 'asc'
			} else if (column.order === 'descending') {
				// 降序
				this.queryData.orderType = 'desc'
			} else {
				delete this.queryData.orderKey
				delete this.queryData.orderType
			}
			this.loadData()
		},
		reset() {
			this.queryData = {
				userName: '',
				applyName: '',
				accountType: [],
				applyType: []
			}
			this.pageNum = 1
			this.formTime.time = [start, end]
			this.loadData()
		},
		filterOperatePage(val) {
			const res = this.operatePage.find((item) => item.code === val)
			return res?.value || '-'
		},
		filterChangeType(val) {
			const res = this.changeType.find((item) => item.code === val)
			return res?.value || '-'
		}
	}
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
	text-align: center;
	color: #909399;
	font-weight: 700;
}
</style>
